package com.lilicould.controller;

import com.lilicould.entity.Account;
import com.lilicould.entity.Employee;
import com.lilicould.entity.EmployeeDetails;
import com.lilicould.entity.Tenant;
import com.lilicould.service.EmployeeService;
import com.lilicould.service.impl.AccountServiceImpl;
import com.lilicould.service.impl.EmployeeServiceImpl;
import com.lilicould.service.impl.TenantServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/login")
public class LoginController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        AccountServiceImpl accountService = new AccountServiceImpl();
        TenantServiceImpl tenantService = new TenantServiceImpl();
        Account account = accountService.login(username, password);


        if (account != null ) {
            // 账号被禁用时禁止登录
            if   (account.getStatus()==0) {
                req.setAttribute("error", "用户被禁用");
                req.getRequestDispatcher("/login.jsp").forward(req, resp);
                return;
            }

            req.getSession().setAttribute("account", account);
            if ("tenant".equals(account.getAccountType())) { // 如果身份显示为租客
                Tenant tenant = accountService.selectTenantByUsername(username); // 查询租客信息
                ArrayList<String> phones = tenantService.selectTenantPhones(tenant.getTenantId());
                req.getSession().setAttribute("tenant", tenant);
                req.getSession().setAttribute("tenantPhones", phones);
            }else if ("employee".equals(account.getAccountType())) { // 如果身份显示为员工
                EmployeeServiceImpl employeeService = new EmployeeServiceImpl();
                ArrayList<EmployeeDetails> employeeDetailsList = employeeService.getAllEmployeeDetails();
                for (EmployeeDetails employeeDetails : employeeDetailsList) { // 遍历员工信息
                    if (employeeDetails.getEmployee().getEmployeeId().equals(account.getUserId())) // 如果员工id等于当前登录的账号id
                        req.getSession().setAttribute("employeeDetails", employeeDetails); // 添加员工信息
                }
            }
            req.getSession().setAttribute("accountType", account.getAccountType()); // 添加身份,转发到index.jsp中
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        }else {
            req.setAttribute("error", "用户名或密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }
    }
}
